<?php
/**
 * Date: 2024.8
 * Author: kahn
 * 订单
 */
use service\vip\OrderService;
use config\Config;
use config\Common;
use config\Response;
use config\Order as OrderConfig;

class OrderController extends  Controller {


    /**
     * @return void
     */
    public function createAction(): void
    {
        $goodsId = $this->_getParam('goodsId','intval');
        $saleId = $this->_getParam('saleId','intval', false);

        $res = OrderService::create($this->userId,$goodsId,$saleId);
        if(is_int($res)){
            $this->_response($res);
        }

        list($orderId, $price) = $res;
        $this->_response(Response::SUCCESS, ['orderId' => $orderId, 'price' => $price]);
    }


    /**
     * 支付订单
     */
    public function payAction(): void
    {
        $payment = $this->_getParam('payment','intval');
        $payWay = $this->_getParam('payWay','intval');
        $orderId = $this->_getParam('orderId','intval');
        $openId = $this->_getParam('openId','string', false, '');

        $res = null;
        try{
            $res = OrderService::pay($this->userId, $orderId, $payment, $payWay, $openId);
        }catch (\Exception $e){
            \Log::error("pay exception ", $e->getMessage());
        }

        if($res === null){
            $this->_response(Response::SYSTEM_ERROR);
        }

        $data = ['payment'=>$payment, 'payWay'=>$payWay, 'payInfo'=>$res];

        $this->_response(Response::SUCCESS, $data);
    }

    /**
     * @return void
     */
    public function queryAction(): void
    {
        $orderId = $this->_getParam('orderId','intval');

        $status = OrderService::query($this->userId, $orderId);
        if($status === null){
            $this->_response(Response::SYSTEM_ERROR);
        }
        $this->_response(Response::SUCCESS, ['status'=>$status]);

    }

    /**
     * @return void
     */
    public function cancelAction(): void
    {
        $orderId = $this->_getParam('orderId','intval');

        $ret = OrderService::cancel($this->userId, $orderId);
        if($ret){
            $this->_response(Response::SUCCESS);
        }else{
            $this->_response('ORDER_CANCEL_FAILED');
        }
    }

    /**
     * 删除订单
     */
    public function deleteAction(): void
    {
        $orderId = $this->_getParam('orderId','intval');

        $ret = OrderService::delete($this->userId, $orderId);
        if($ret){
            $this->_response(Response::SUCCESS);
        }else{
            $this->_response('ORDER_DELETED_FAILED');
        }
    }

    /**
     * 订单列表
     */
    public function listAction(): void
    {
        $list = OrderService::list($this->userId);
        $this->_response(Response::SUCCESS, ['list'=>$list]);
    }

}